package com.foreverwin.mes.lims.service.impl;

import com.foreverwin.mes.common.constants.HandleBOConstants;
import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.lims.dto.InspectFinishedProductDto;
import com.foreverwin.mes.lims.enums.InspectType;
import com.foreverwin.mes.lims.model.InspectFinishedProduct;
import com.foreverwin.mes.lims.mapper.InspectFinishedProductMapper;
import com.foreverwin.mes.lims.service.InspectFinishedProductService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Levy
 * @since 2020-03-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InspectFinishedProductServiceImpl extends ServiceImpl<InspectFinishedProductMapper, InspectFinishedProduct> implements InspectFinishedProductService {


    @Autowired
    private InspectFinishedProductMapper inspectFinishedProductMapper;

    @Override
    public Page<InspectFinishedProduct> selectPage(FrontPage<InspectFinishedProduct> frontPage, InspectFinishedProduct inspectFinishedProduct) {
        EntityWrapper<InspectFinishedProduct> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(inspectFinishedProduct);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<InspectFinishedProduct> selectList(InspectFinishedProduct inspectFinishedProduct) {
        EntityWrapper<InspectFinishedProduct> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(inspectFinishedProduct);
        return super.selectList(queryWrapper);
    }

    @Override
    public List<InspectFinishedProduct> getInspectFinishedProduct(InspectFinishedProductDto inspectFinishedProductDto) {
        String site = CommonMethods.getSite();
        Date startTime = inspectFinishedProductDto.getStartTime();
        Date endTime =  inspectFinishedProductDto.getEndTime();
        String sfc = inspectFinishedProductDto.getSfc();
        String product = inspectFinishedProductDto.getProduct();
        String inspector = inspectFinishedProductDto.getInspector();
        List<InspectFinishedProduct> finishedProducts = new ArrayList<>();

        List<InspectFinishedProduct> inspectFinishedProducts = inspectFinishedProductMapper.getInspectFinishedProduct(
                sfc , site , product , InspectType.PT.getCode()
                , inspector , startTime , endTime );

        //去除已经完成成品放行的数据
        for ( InspectFinishedProduct inspectFinishedProduct : inspectFinishedProducts ){
            String productBo =  inspectFinishedProduct.getProduct();
            inspectFinishedProduct.setProduct( productBo.split("/")[0] );
            inspectFinishedProduct.setProductDesc( productBo.split("/")[1] );
            String bo = StringUtils.genHandle(HandleBOConstants.INSPECT_FINISHED_PRODUCT_BO , site , inspectFinishedProduct.getTaskId());
            InspectFinishedProduct finishedProduct =  inspectFinishedProductMapper.selectById( bo );
            if ( finishedProduct  == null ){
                finishedProducts.add( inspectFinishedProduct );
            }
        }

        return finishedProducts;
    }
}